package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.CategoryCreateDto;
import com.example.front.dto.CategoryDto;
import com.example.front.dto.CategorySearchDto;
import com.example.front.entity.Button;
import com.example.front.entity.Category;
import com.example.front.entity.Gateway;
import com.example.front.mapper.CategoryMapper;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    ButtonService buttonService;

    @Resource
    BoxService boxService;

    /**
     * 列出盒子类型
     */
    public ListDto<CategoryDto> list(CategorySearchDto condition, Pagination pagination) {

        long total = categoryMapper.count(condition);
        if (total <= 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }

        pagination.setTotal(total);

        List<Category> categoryList = categoryMapper.findAll(condition, pagination);

        List<CategoryDto> gatewayDtoList = categoryList.stream().map(category -> {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtils.copyProperties(category, categoryDto);
            return categoryDto;
        }).collect(Collectors.toList());

        return new ListDto<>(gatewayDtoList, pagination);
    }

    /**
     * 创建盒子类型
     */
    @Transactional
    public void create(@Valid CategoryCreateDto categoryCreateDto) {
        User me = UserContext.getUser();

        if (categoryMapper.findByName(me.getTenantId(), categoryCreateDto.getName()) != null) {
            throw new BusinessException("名称已存在");
        }

        Category category = new Category();
        category.setTenantId(me.getTenantId());
        BeanUtils.copyProperties(categoryCreateDto, category);

        categoryMapper.create(category);
    }

    /**
     * 修改盒子类型
     */
    public void update(@Valid CategoryDto categoryDto) {
        User me = UserContext.getUser();

        if (categoryMapper.findByName(me.getTenantId(), categoryDto.getName()) != null) {
            throw new BusinessException("名称已存在");
        }

        Category category = new Category();
        category.setTenantId(me.getTenantId());
        BeanUtils.copyProperties(categoryDto, category);
        if (categoryMapper.update(category) != 1) {
            throw new BusinessException("操作失败");
        }
    }

    /**
     * 删除盒子类型
     */
    @Transactional
    public void deleteById(long id, long tenantId) {
        if (boxService.findByCategoryId(tenantId, id) != 0) {
            throw new BusinessException("该盒子类型下存在有按钮盒，请将该盒子类型下的按钮盒删除或修改再执行该操作");
        }
        if (categoryMapper.deleteById(id, tenantId) != 1) {
            throw new BusinessException("操作失败");
        }
        Button button = Button.builder().tenantId(tenantId).categoryId(id).build();
        buttonService.deleteByCategoryId(button);
    }

    /**
     * 搜索盒子类型
     */
    public Category findByName(long tenantId, String name) {

        if (categoryMapper.findByName(tenantId, name) == null) {
            throw new BusinessException("型号不存在");
        }
        return categoryMapper.findByName(tenantId, name);
    }

    /**
     * 检查盒子是否存在
     */
    public void findById(Long tenantId, Long id) {
        if (categoryMapper.findById(tenantId, id) == null) {
            throw new BusinessException("盒子类型不存在");
        }
    }

    /**
     * 获取盒子类型信息
     */
    public Map<Long, Category> findByIds(List<Long> categoryIds) {

        Map<Long, Category> categoryMap = categoryMapper.findCategoryByIds(categoryIds).stream()
                .collect(Collectors.toMap(Category::getId, category -> category));

        return categoryMap;
    }


    /**
     * 获取盒子类型信息
     */
    public Category findbyId(Long id) {
        return categoryMapper.findbyId(id);
    }



    public Map<String,String> categoryEvent(Long id,Integer key){
        return categoryMapper.categoryEvent(id,key);
    }

}
